package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IRouterMapper;
import com.cuixichang.minimalism.transfer.dto.IRouterDTO;
import com.cuixichang.minimalism.transfer.factory.IRouterAssembler;
import com.cuixichang.minimalism.transfer.po.IRouter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class IRouterServiceImpl implements IRouterService {

    @Autowired
    private IRouterMapper iRouterMapper;

    @Override
    public IRouterDTO selectById(Long id){
        IRouter iRouter = iRouterMapper.selectById(id);
        return IRouterAssembler.INSTANCT.convertDto(iRouter);
    }

    @Override
    public IRouterDTO selectOne(IRouterDTO iRouterDTO){
        Assert.notNull(iRouterDTO,"参数为空,请检查");
        IRouter iRouter = IRouterAssembler.INSTANCT.convertPo(iRouterDTO);
        IRouter result = iRouterMapper.selectOne(new QueryWrapper<>(iRouter));
        return IRouterAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IRouterDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IRouter> results = iRouterMapper.selectBatchIds(ids);
        return results.stream().map(IRouterAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IRouterDTO> selectList(IRouterDTO iRouterDTO){
        return this.selectList(iRouterDTO, (iRouter, queryWrapper) -> {});
    }

    @Override
    public List<IRouterDTO> selectList(IRouterDTO iRouterDTO, BiConsumer<IRouter, QueryWrapper<IRouter>> consumer){
        Assert.notNull(iRouterDTO,"参数为空,请检查");
        IRouter iRouter = IRouterAssembler.INSTANCT.convertPo(iRouterDTO);
        QueryWrapper<IRouter> queryWrapper = new QueryWrapper<>(iRouter);
        if(Objects.nonNull(consumer)){
            consumer.accept(iRouter, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IRouter> results = iRouterMapper.selectList(queryWrapper);
        return results.stream().map(IRouterAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IRouterDTO> selectPage(IRouterDTO iRouterDTO,long pageSize,long pageNo){
        return this.selectPage(iRouterDTO, pageSize, pageNo, (iRouter, queryWrapper) -> {});
    }

    @Override
    public Page<IRouterDTO> selectPage(IRouterDTO iRouterDTO,long pageSize,long pageNo, BiConsumer<IRouter, QueryWrapper<IRouter>> consumer){
        Assert.notNull(iRouterDTO,"参数为空,请检查");
        IRouter iRouter = IRouterAssembler.INSTANCT.convertPo(iRouterDTO);
        QueryWrapper<IRouter> queryWrapper = new QueryWrapper<>(iRouter);
        if(Objects.nonNull(consumer)) consumer.accept(iRouter, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IRouter> page = iRouterMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IRouterDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IRouterAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iRouterMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iRouterMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IRouterDTO iRouterDTO){
        return deleteByCondition(iRouterDTO, (iRouter, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IRouterDTO iRouterDTO, BiConsumer<IRouter, QueryWrapper<IRouter>> consumer){
        Assert.notNull(iRouterDTO,"参数为空,请检查");
        IRouter iRouter = IRouterAssembler.INSTANCT.convertPo(iRouterDTO);
        QueryWrapper<IRouter> queryWrapper = new QueryWrapper<>(iRouter);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iRouter, queryWrapper);
        }
        return iRouterMapper.delete(queryWrapper);
    }

    @Override
    public IRouterDTO insert(IRouterDTO iRouterDTO){
        return insertHasOption(iRouterDTO, (IRouterDTO) -> null);
    }

    @Override
    public IRouterDTO insertHasOption(IRouterDTO iRouterDTO, Function<IRouterDTO,IRouterDTO> preOption){
        Assert.notNull(iRouterDTO,"参数为空,请检查");
        iRouterDTO.check();
        IRouter iRouter = IRouterAssembler.INSTANCT.convertPo(iRouterDTO);
        Long createLoginId = iRouter.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRouter.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iRouter.setCreateTime(localDateTime);
        iRouter.setUpdateTime(localDateTime);
        Boolean isEnable = iRouter.getIsEnable();
        if(Objects.isNull(isEnable)) iRouter.setIsEnable(true);
        Integer sort = iRouter.getSort();
        if(Objects.isNull(sort)) iRouter.setSort(0);
        if(Objects.nonNull(preOption)){
            IRouterDTO apply = preOption.apply(iRouterDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iRouterMapper.insert(iRouter);
        Assert.isTrue(insert>0,"插入失败");
        return IRouterAssembler.INSTANCT.convertDto(iRouter);
    }

    @Override
    public int updateById(IRouterDTO iRouterDTO){
         return updateById(iRouterDTO, (iRouter, updateWrapper) -> {});
    }

    @Override
    public int updateById(IRouterDTO iRouterDTO, BiConsumer<IRouter, UpdateWrapper<IRouter>> consumer){
        Assert.notNull(iRouterDTO,"参数为空,请检查");
        Long id = iRouterDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IRouter iRouter = IRouterAssembler.INSTANCT.convertPo(iRouterDTO);
        Long createLoginId = iRouter.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRouter.setCreateLoginId(null);
        iRouter.setUpdateLoginId(createLoginId);
        iRouter.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IRouter> updateWrapper = new UpdateWrapper<IRouter>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iRouter,updateWrapper);
        return iRouterMapper.update(iRouter,updateWrapper);
    }
}
